package com.survivor.menu;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.survivor.activity.CoordHelper;
import com.survivor.activity.TextureLoader;
import com.survivor.linklines.R;

public class MenuMoving {
	
//	R.drawable.background,
	private int[] picIDs = { 
			R.drawable.background,
			R.drawable.black_ball, R.drawable.blue_ball, R.drawable.gray_ball,
					R.drawable.green_ball, R.drawable.light_gray_ball,
					R.drawable.pink_ball, R.drawable.red_ball, R.drawable.yellow_ball,

					R.drawable.list_effect1, R.drawable.list_effect2};
	
	private CoordHelper ch;
	
	private List<Bitmap> bmpArray = new ArrayList<Bitmap>();
	
	private float[] bgVerCoord = {-3f, -3f, 0, 1f, -3f, 0, -3f, 1f, 0, 1f, 1f, 0,};
	private float[] bgTexCoord;
	
	private float[] titleVerCoord = {-.5f, -.1f, 0, .5f, -.1f, 0, -.5f, .8f, 0, .5f, .8f, 0};
	private float[] ballVerCoord = {-.15f, -.25f, 0, .15f, -.25f, 0, -.15f, .25f, 0, .15f, .25f, 0};
	
	private float[] titleTexCoord = {0, 1, 1, 1, 0, 0, 1, 0};
	private float[] ballTexCoord = {0, 1, 1, 1, 0, 0, 1, 0};
	
	
	private FloatBuffer allVerBuffer, allTexBuffer;
	
	private int[] textures = new int[picIDs.length];
	
	public MenuMoving(Context context, CoordHelper ch) {
		// TODO Auto-generated constructor stub
		
		this.ch = ch;
		
		for(int i = 0; i < picIDs.length; i++)
			bmpArray.add(BitmapFactory.decodeResource(context.getResources(), picIDs[i]));
		
		initCoord();
		makeFloatBuffer();
	}
	
	private void initCoord()
	{
		int oneBgWidth = bmpArray.get(0).getWidth();
		int oneBgHeight = bmpArray.get(0).getHeight();
		float bgTexCoordX = getCount((ch.w * 2f) / oneBgWidth);
		float bgTexCoordY = getCount((ch.h * 2f) / oneBgHeight);
		System.out.println(bgTexCoordX);
		System.out.println(bgTexCoordY);
		
		bgTexCoord = new float[] { 0, bgTexCoordY, bgTexCoordX, bgTexCoordY, 0, 0, bgTexCoordX, 0};
	}
	
	
	private void makeFloatBuffer()
	{
		ByteBuffer verBb = ByteBuffer.allocateDirect((bgVerCoord.length + ballVerCoord.length + titleVerCoord.length) * 4);
		verBb.order(ByteOrder.nativeOrder());
		allVerBuffer = verBb.asFloatBuffer();
		allVerBuffer.put(bgVerCoord);
		allVerBuffer.put(ballVerCoord);
		allVerBuffer.put(titleVerCoord);
		
		allVerBuffer.position(0);
		
		ByteBuffer texBb = ByteBuffer.allocateDirect((bgTexCoord.length + ballTexCoord.length + titleTexCoord.length) * 4);
		texBb.order(ByteOrder.nativeOrder());
		allTexBuffer = texBb.asFloatBuffer();
		allTexBuffer.put(bgTexCoord);
		allTexBuffer.put(ballTexCoord);
		allTexBuffer.put(titleTexCoord);
		
		allTexBuffer.position(0);
	}
	
	private byte[] verCoordIndex = new byte[4];
	private float transX = 0f, transY = 0f;
	
	public void drawBackground(GL10 gl)
	{
		gl.glLoadIdentity();
		
		gl.glColor4f(1, 1, 1, 1);
		gl.glTranslatef(transX, transY, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		for (int i = 0; i < 4; i++)
			verCoordIndex[i] = (byte) i;
		gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE,
				ByteBuffer.wrap(verCoordIndex));
		transX += 0.002f;
		transY += 0.002f;
		if (transY >= 2f) {
			transX -= 2.002f;
			transY -= 2.002f;
		}
	}
	
	private int ballIndex = 1;
	private int ballFrame = 0;
	
	public void drawBall(GL10 gl)
	{
		gl.glLoadIdentity();

		gl.glColor4f(1f, 1f, 1f, .8f);
		if (ballFrame == 10) {
			ballIndex += 1;
			ballFrame = 0;
			if (ballIndex >= textures.length - 2)
				ballIndex = 1;
		}
		gl.glTranslatef(0, -.4f, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[ballIndex]);
		for (int i = 0; i < 4; i++)
			verCoordIndex[i] = (byte) (i + 4);
		gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE,
				ByteBuffer.wrap(verCoordIndex));
		ballFrame++;
		
		gl.glColor4f(1f, 1f, 1f, 1f);
	}
	
	private int titleIndex = 9;
	private int titleFrame = 0;

	public void drawTitleBold(GL10 gl) {
		gl.glLoadIdentity();
		gl.glColor4f(1f, 1f, 1f, 1f);
		if (titleFrame == 8) {
			titleIndex += 1;
			titleFrame = 0;
			if (titleIndex >= textures.length)
				titleIndex = 9;
		}
		gl.glTranslatef(0, .1f, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[titleIndex]);
		for (int i = 0; i < 4; i++)
			verCoordIndex[i] = (byte) (i + 8);
		gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 4, GL10.GL_UNSIGNED_BYTE,
				ByteBuffer.wrap(verCoordIndex));
		titleFrame++;
	}
	
	public void setFloatBuffer(GL10 gl)
	{
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, allTexBuffer);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, allVerBuffer);
	}
	
	
	public void loadTextures(GL10 gl)
	{
		TextureLoader.loadTextures(gl, bmpArray, textures);
	}
	
	private int getCount(float f)
	{
		if(f >= 15)
			return 20;
		else
			return 10;
	}
	

}
